import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

import login from '@/views/login.vue'

import admin from '@/views/admin.vue'
import owners from '@/views/owners/owners.vue'
import Car from '@/views/owners/car.vue'
import Pet from '@/views/owners/pet.vue'
import Parking  from '@/views/park/parking.vue'
import parkingUse  from '@/views/park/parkingUse.vue'
import HouseView from '@/views/build/house'
import BuildingView from '@/views/build/building'
import HouseingView from '@/views/build/house'
import Community from '@/views/community/community'
import activity from '@/views/service/activity'
import complaints from '@/views/service/complaints'
import mail from '@/views/service/mail'
import warranty from '@/views/service/warranty'
import assets from '@/views/assets/assets'
import charge from '@/views/charge/charge'
import detail from '@/views/charge/detail'
import administrator from '@/views/administrator/administrator'
import users from '@/views/administrator/users'
import permissions from '@/views/administrator/permissions'
import systemSettings from '@/views/setting/systemSettings'
import personalSettings from '@/views/setting/personalSettings'
import logPrint from '@/views/log/logPrint'
import chart from '@/views/chart'
import register from '@/views/register'
import Vuex from 'vuex';
Vue.use(Vuex);
const store = new Vuex.Store({
    state: {
        // 存储token
        LINGPAI: localStorage.getItem('LINGPAI') ? localStorage.getItem('LINGPAI') : ''
    },
    mutations: {
        // 修改token，并将token 存入localStorage中
        changeLogin(state, user) {
            state.LINGPAI = user.LINGPAI;
            localStorage.setItem('LINGPAI', user.LINGPAI);
        }
    }
});
// export default store;


Vue.use(Router);
const routes = [{
        path: '/',
        component: login
    },
    {
    path: '/register',
    component: register
    },
    {
        path: '/admin',
        component: admin,
        children: [
            {path:'/',component:chart},
            {path:'community',component:Community},
            { path: 'owners', component: owners },
            { path: 'car', component: Car },
            { path: 'pet', component: Pet },
            { path: 'park', component: Parking },
            { path: 'parkingUse', component: parkingUse },
            { path: 'hello', component: HelloWorld },
            {path:'park',component:HouseView},
            {path:'manage',component: HouseingView },
            {path:'build',component:BuildingView},
            {path:'activity',component:activity},
            {path:'complaints',component:complaints},
            {path:'mail',component:mail},
            {path:'warranty',component:warranty},
            {path:'assets',component:assets},
            {path:'detail',component:detail},
            {path:'charge',component:charge},
            {path:'permissions',component:permissions},
            {path:'users',component:users},
            {path:'administrator',component:administrator},
            {path:'personalSettings',component:personalSettings},
            {path:'systemSettings',component:systemSettings},
            {path:'logPrint',component:logPrint},
        ]
    }, {
        path: '/hello',
        component: HelloWorld
    }
];
const router = new Router({
    mode: 'history',
    routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.path == '/' || to.path == '/register') {
        next();
    } else {
        // 获取当前用户的令牌
        let token = localStorage.getItem('Authentication');
        if (token === null || token === '') {
            alert("登录已过期,请前往登录!")
            next('/');
        } else {
            next();
        }
    }
});
export default router;